package lt.ntec.danblog.server.blog;

import java.util.Date;
import javax.annotation.Nullable;
import javax.inject.Inject;
import lt.ntec.danblog.server.blog.model.BlogEntityFactory;
import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.blog.model.PostDAO;
import lt.ntec.danblog.server.core.CommentService;
import lt.ntec.danblog.server.core.model.Comment;
import lt.ntec.danblog.server.core.model.CommentDAO;
import lt.ntec.danblog.server.core.model.User;
import lt.ntec.danblog.server.core.validator.ValidationException;

class BlogCommentService implements CommentService {

    @Inject
    private CommentDAO commentDAO;
    @Inject
    @Nullable
    private User user;
    @Inject
    private BlogEntityFactory blogEntityFactory;
    @Inject
    private PostDAO postDAO;

    @Override
    public void removeComment(Long id) {
        commentDAO.delete(id);
    }

    @Override
    public void updateComment(Long id, String content)
            throws
            ValidationException {

        Comment comment = commentDAO.getById(id);
        comment.setContent(content);

        commentDAO.save(comment);

    }

    @Override
    public void rateUpComment(Long id) {
        Comment comment = commentDAO.getById(id);
        comment.addVoteUp(user);
        commentDAO.save(comment);

    }

    @Override
    public void rateDownComment(Long id) {
        Comment comment = commentDAO.getById(id);
        comment.addVoteDown(user);
        commentDAO.save(comment);
    }

    @Override
    public Long addComment(Long parentId, String content)
            throws ValidationException {
        Post post = postDAO.getById(parentId);
        Comment comment = blogEntityFactory.createComment(user, post);
        comment.setDate(new Date());
        comment.setContent(content);
        return commentDAO.save(comment);
    }

    @Override
    public Comment getComment(Long id) {
        return commentDAO.getById(id);
    }
}
